﻿using System.Linq;
using System.Linq.Expressions;

namespace Smart.Admin.Domain.Common
{
    public static class QueryableExtensions
    {
        public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
        {
            return condition ? query.Where(predicate) : query;
        }

        public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, int, bool>> predicate)
        {
            return condition ? query.Where(predicate) : query;
        }

        public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> query, bool condition, Func<T, bool> predicate)
        {
            return condition ? query.Where(predicate) : query;
        }

        public static IQueryable<T> Where<T>(this IQueryable<T> query, List<Expression<Func<T, bool>>> expression)
        {
            if (expression != null)
            {
                expression.ForEach(m => { query = query.Where(m); });
            }
            return query;
        }
    }
}
